<?php
class ProjectAction extends Action{
	//作品展示展示
	public function projectList(){
		//组装条件
		$where="";
		
		//方案名称
		$name = trim($this->_post('projectName'));
		if(!empty($name)){
			$where.=" and p.Name like '%".$name."%'";
		}
		
		//团队名称
		$teamname = trim($this->_post('teamName'));
		if(!empty($teamname)){
			$where.=" and t.Name like '%".$teamname."%'";
		}
		
		//院校名称
		$schoolname = trim($this->_post('schoolName'));
		if(!empty($schoolname)){
			$where.=" and u.OrgName like '%".$schoolname."%'";
		}
		
		//方案组别
		$ddltheme = $this->_post('ddlTheme');
		if(!empty($ddltheme) && $ddltheme!=-1){
			
			$where.=" and pt.ThemeId=".$ddltheme;
		}
		
		//比赛阶段
		$ddlsection = $this->_post('ddlSection');
		if(!empty($ddlsection) && $ddlsection!=-1){
			$where.=" and t.Status=".$ddlsection;
		}
		import('ORG.Util.Page');// 导入分页类
		$count = M('')->query("select count(*) as num from project p,team t,projecttheme pt,users u where t.Captain=u.UserId and p.TeamId=t.Id and p.Id=pt.ProjectId ".$where);// 查询满足要求的总记录数
		//var_dump($count);
		$Page = new Page($count[0]['num'],20);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = M('')->limit($Page->firstRow.','.$Page->listRows)->query("select p.Id,p.Name,p.Summarize,t.Name tName,u.TrueName,u.OrgName,p.CreateTime,p.CommentCount,p.ViewCount,p.Poll,p.IsPublic,t.Poll tPoll,t.Status,t.Id tid,u.UserId uid from project p,team t,projecttheme pt,users u where t.Captain=u.UserId and p.TeamId=t.Id and p.Id=pt.ProjectId ".$where." order by t.Status desc");
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);
		// echo '<pre>';
		// var_dump($list);
		//人气团队
		$team = M('')->query("select t.Id,t.Name,t.Poll,p.Name pname,p.Id pid from team t,project p where t.Id=p.TeamId order by t.Poll desc limit 5");
		$this->assign('team',$team);
		$this->display();
	}
	
	//方案展示
	public function projectShow(){
		$pid = $this->_get('projectid');
		//浏览数+1
		M('Project')->where('Id='.$pid)->setInc('ViewCount');
		//判断是否公开
		$project = M('Project')->where('Id='.$pid)->find();
		$this->assign('project',$project);
		$limit = M('Projectcommentlimit')->where('ProjectId='.$pid)->select();
		$createuser = M()->query("select u.UserId from project p,team t,users u where t.Captain=u.UserId and p.TeamId=t.Id and p.Id=".$pid);
		//当前登录用户的uid
		$uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		//方案评分判断是否评委
		$jury = M('Users')->where('IsJury=1 and UserId='.$uid)->find();
		$this->assign('jury',$jury);
		//权限限制
		if(!empty($limit)){
			foreach($limit as $row){
				$permit[] = $row['UserId'];
			}
		}
		$permit[] = $createuser[0]['UserId'];
		$permit[] = $jury['UserId'];
		if(in_array($uid,$permit)){
			$this->assign('premit',1);
		}
		
		//方案
		$proinfo = M()->query("select u.UserId,u.TrueName,t.Name tName,t.Status,p.Name pName,p.Summarize,p.Divide,p.ChoiceEx,p.LastUpdate,p.CreateTime,p.TeamId from users u,team t,project p where u.UserId=t.Captain and t.Id=p.TeamId and p.Id=".$pid);
		//所选问题
		$problem = M()->query("select b.Title,b.Goal,b.Description from project p,businessproblems b where p.ProblemId=b.Id and p.Id=".$pid);
		//实践作品展示
		$workshow = M()->query("select w.Name,w.Platform,w.Url,w.Description from works w where w.ProjectId=".$pid);//works
		//附件和主题赛类型
		$file = M()->query("select pf.Id,pf.FileName,pf.OldName from project p,projectattachfile pf where pf.ProjectId=p.Id and p.Id=".$pid);
		//主题赛类型
		$theme = M()->query("select t.Id,t.Name from project p,projecttheme pt,theme t where t.Id=pt.ThemeId and  p.Id=pt.ProjectId and p.Id=".$pid);
		$this->assign('proinfo',$proinfo[0]);
		$this->assign('problem',$problem[0]);
		$this->assign('workshow',$workshow);
		$this->assign('file',$file);
		$this->assign('theme',$theme[0]);
		$this->assign('proid',$this->_get('projectid'));
		$this->display();
	}
	//下载
	public function downLoad(){
		$file = M('Projectattachfile')->where('Id='.$this->_get('id'))->find();
		//echo "UpFile/Doc/UploadFile/{$file['FileName']}";
		//告诉浏览器字符集
		header('content-type:charset=utf-8');
		//告诉浏览器你的文件MIME类型
		//header('content-type:image/jpeg');
		//告诉浏览器你的文件描述
		header('Content-Disposition:attachment;filename="'.$file['OldName'].'"');
		//告诉浏览器文件的大小
		header('content-length:filesize(UpFile/Doc/UploadFile/'.$file[FileName].')');
		//显示内容
		readfile("UpFile/Doc/UploadFile/{$file['FileName']}");
	}
	//投票
	public function dopost(){
		$this->ajaxReturn('OK','投票成功',1);
	}
	//可以评分的团队方案
	public function juryTeams(){
		$team = new CreatTeamModel();
		$this->assign('newteam',$team->newteam);//最新团队显示;
		$this->assign('mostvoteteam',$team->mostvoteteam);//投票最多团队;
		$this->assign('hotteam',$team->hotteam);//热门评论团队;
		$this->assign('mostteacher',$team->mostteacher);//最新评论老师/教官;
		//SQL判断有权给那些方案评分
		import('ORG.Util.Page');
		$count = M()->query("select count(*) num from jury j,team t,project p where t.Id=p.TeamId and j.JurySchools=t.SchoolId and j.UserId=".$_SESSION[C('USER_AUTH_KEY')]['UserId']);
		$page = new Page($count[0]['num'],20);
		$show = $page->show();
		$list = M()->limit($page->firstRow.','.$page->listRows)->query("select p.Id,p.Name pname,p.Poll,p.Summarize,p.CreateTime,p.CommentCount,p.ViewCount,t.Name tname,t.Status,t.CaptainName from jury j,team t,project p where t.Id=p.TeamId and j.JurySchools=t.SchoolId and j.UserId=".$_SESSION[C('USER_AUTH_KEY')]['UserId']);
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}
	//处理评委评分
	public function grade(){
		$themeid = $this->_get('themeid');
		$data['PublicScor'] = $this->_post('degree') + $this->_post('innovateres') + $this->_post('workshow') + $this->_post('teamwork');
		switch($themeid){
			case 1:
				$data['ThemeScore'] = $this->_post('tool') + $this->_post('bunderstand') + $this->_post('innovate') + $this->_post('goal') + $this->_post('mean') + $this->_post('extend');
				break;
			case 2:
				$data['ThemeScore'] = $this->_post('tool') + $this->_post('bunderstand') + $this->_post('innovate');
				break;
			case 3:
				$data['ThemeScore'] = $this->_post('bstrategy') + $this->_post('mstrategy');
				break;
			case 4:
				$data['ThemeScore'] = $this->_post('blogic') + $this->_post('network');
				break;
			case 5:
				$data['ThemeScore'] = $this->_post('binnovate_f') + $this->_post('binnovate_s');
				break;
			case 6:
				$data['ThemeScore'] = $this->_post('iunderstand') + $this->_post('estrategy');
				break;
		}
		$pid = $this->_post('pid');
		$uid = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$data['Jury'] = $uid;
		$count = M()->query("select count(*) num from juryproject j where j.ProjectId=".$pid);
		//echo $count[0]['num'];
		//$count[0]['num']=4;
		$arr = M('Juryproject')->where("ProjectId=".$pid." and Jury=".$uid)->find();
		//判断权限
		$list = M()->query("select p.Id,p.Name pname,p.Poll,p.Summarize,p.CreateTime,p.CommentCount,p.ViewCount,t.Name tname,t.Status,t.CaptainName from jury j,team t,project p where t.Id=p.TeamId and j.JurySchools=t.SchoolId and j.UserId=".$uid." and p.Id=".$pid);
		if($list){
			if($count[0]['num'] >= 3){
				echo '<script>alert("该方案评分已经3次，你不能评分了！");window.history.back()</script>';
			}elseif($count[0]['num'] == 1){
				$res = M('Juryproject')->where("ProjectId=".$pid." and Jury=".$uid)->find();
				if($res && $arr['PublicScor'] !=''){
					echo '<script>alert("你已经评分过了，不能重复评分！");window.history.back()</script>';
				}else{
					$copy = M('Juryproject')->where("ProjectId=".$pid)->find();
					unset($copy['Id']);
					$jid = M('Juryproject')->add($copy);
					$res = M('Juryproject')->where("Id=".$jid)->save($data);
					if($res){
						echo '<script>alert("评分成功！");window.history.back()</script>';
					}else{
						echo '<script>alert("评分失败！");window.history.back()</script>';
					}
				}
			}else{
				$res = M('Juryproject')->where("ProjectId=".$pid." and Jury=".$uid)->find();
				if($res){
					echo '<script>alert("你已经评分过了，不能重复评分！");window.history.back()</script>';
				}else{
					$copy = M('Juryproject')->where("ProjectId=".$pid)->find();
					unset($copy['Id']);
					$jid = M('Juryproject')->add($copy);
					$res = M('Juryproject')->where("Id=".$jid)->save($data);
					if($res){
						echo '<script>alert("评分成功！");window.history.back()</script>';
					}else{
						echo '<script>alert("评分失败！");window.history.back()</script>';
					}
				}
			}
		}else{
			echo '<script><script>alert("你没有权限！");window.history.back()</script></script>';
		}
	}
	
}